package com.dwb.service;

import com.dwb.dto.SGoodsDto;
import com.dwb.entity.SGoods;
import com.dwb.entity.SGoodsCategoryJoin;
import com.dwb.entity.SGoodsPhoto;
import com.dwb.mappers.SGoodsCategoryJoinMapper;
import com.dwb.mappers.SGoodsMapper;
import com.dwb.mappers.SGoodsPhotoMapper;
import com.dwb.vo.TestVo;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.models.auth.In;
import org.apache.commons.codec.binary.Base32OutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.dwb.common.check.Check.*;
import static com.dwb.common.check.Check.CHECK_GOODS_UPDATE_STATE;

@Service
public class SGoodsService {
    @Autowired
    private SGoodsMapper sGoodsMapper;
    @Autowired
    private SGoodsPhotoMapper sGoodsPhotoMapper;
    @Autowired
    private SGoodsCategoryJoinMapper sGoodsCategoryJoinMapper;

    @Transactional
    public void insert(Map<String, Object> parameters) {
        Object sGoods = parameters.get("sGoods");
        ObjectMapper objectMapper = new ObjectMapper();
        SGoods sGoodsV = objectMapper.convertValue(sGoods, SGoods.class);
        sGoodsV.setIsExistManySpec(false);
        //服务产品插入
        sGoodsMapper.insert(sGoodsV);
        // 商品图片插入
        List<Object> sGoodsPhotos = (List<Object>) parameters.get("sGoodsPhotos");
        for (Object sGoodsPhoto : sGoodsPhotos) {
            SGoodsPhoto sGoodsPhotoV = objectMapper.convertValue(sGoodsPhoto, SGoodsPhoto.class);
            sGoodsPhotoV.setGoodsId(sGoodsV.getId());
            sGoodsPhotoMapper.insert(sGoodsPhotoV);
        }
        // 商品分类插入
        Object sGoodsCategoryJoin = parameters.get("sGoodsCategoryJoins");
        SGoodsCategoryJoin sGoodsCategoryJoinV = objectMapper.convertValue(sGoodsCategoryJoin, SGoodsCategoryJoin.class);
        sGoodsCategoryJoinV.setGoodsId(sGoodsV.getId());
        sGoodsCategoryJoinMapper.insert(sGoodsCategoryJoinV);
    }

    @Transactional
    public void update(Map<String, Object> parameters) {
        Object sGoods = parameters.get("sGoods");
        ObjectMapper objectMapper = new ObjectMapper();
        SGoods sGoodsV = objectMapper.convertValue(sGoods, SGoods.class);
        sGoodsMapper.updateByPrimaryKeySelective(sGoodsV); //更新服务产品基本信息表
        List<SGoodsPhoto> sGoodsPhotos = (List<SGoodsPhoto>) parameters.get("sGoodsPhotos");
        // 删除图片
        Example example = new Example(SGoodsPhoto.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("goodsId", sGoodsV.getId());
        sGoodsPhotoMapper.deleteByExample(example);

        for (int i = 0; i < sGoodsPhotos.size(); i++) {
            Object sGoodsPhoto = sGoodsPhotos.get(i);
            SGoodsPhoto sGoodsPhotoV = objectMapper.convertValue(sGoodsPhoto, SGoodsPhoto.class);
            sGoodsPhotoV.setGoodsId(sGoodsV.getId());
            sGoodsPhotoMapper.insert(sGoodsPhotoV);
        }
//        for (SGoodsPhoto goodsPhoto : sGoodsPhotoV) {
//            goodsPhoto.setGoodsId(sGoodsV.getId());
//            sGoodsPhotoMapper.insert(goodsPhoto);
//
//        }
        // 修改分类
        Object sGoodsCategoryJoin = parameters.get("sGoodsCategoryJoins");
        SGoodsCategoryJoin sGoodsCategoryJoinV = objectMapper.convertValue(sGoodsCategoryJoin, SGoodsCategoryJoin.class);

        Example exampleV = new Example(SGoodsCategoryJoin.class);
        Example.Criteria criteria1 = exampleV.createCriteria();
        criteria1.andEqualTo("goodsId", sGoodsV.getId());
        List<SGoodsCategoryJoin> sGoodsCategoryJoins = sGoodsCategoryJoinMapper.selectByExample(exampleV);
        sGoodsCategoryJoinV.setId(sGoodsCategoryJoins.get(0).getId());
        sGoodsCategoryJoinV.setGoodsId(sGoodsV.getId());
        sGoodsCategoryJoinMapper.updateByPrimaryKeySelective(sGoodsCategoryJoinV);

    }

    public Map<String, Object> getInfo(Map<String, String> parameters) {

        //获取产品服务基本信息
        SGoods sGoods = sGoodsMapper.selectByPrimaryKey(Integer.parseInt(parameters.get("id")));
        //获取产品服务图片
        Example example = new Example(SGoodsPhoto.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("goodsId", sGoods.getId());
        List<SGoodsPhoto> sGoodsPhotos = sGoodsPhotoMapper.selectByExample(example);
        // 获取产品服务分类
        Example exampleSGoodsCategoryJoin = new Example(SGoodsCategoryJoin.class);
        Example.Criteria criteria1 = exampleSGoodsCategoryJoin.createCriteria();
        criteria1.andEqualTo("goodsId", sGoods.getId());
        List<SGoodsCategoryJoin> sGoodsCategoryJoins = sGoodsCategoryJoinMapper.selectByExample(exampleSGoodsCategoryJoin);
        HashMap<String, Object> map = new HashMap<>();
        map.put("sGoods", sGoods);
        map.put("sGoodsPhotos", sGoodsPhotos);
        map.put("sGoodsCategoryJoins", sGoodsCategoryJoins);
        return map;


    }

    public PageInfo<SGoodsDto> getList(Map<String, String> parameters) {

        CHECK_GOODS_GET_LIST(parameters);
        PageHelper.startPage(Integer.parseInt(parameters.get("pageNum") == null ? "1" : parameters.get("pageNum")),
                Integer.parseInt(parameters.get("pageSize")==null ? "10" : parameters.get("pageSize")));
//        Example example = new Example(SGoods.class);
//        Example.Criteria criteria = example.createCriteria();
//        criteria.andLike("title","%"+(parameters.get("title") == null ? "" : parameters.get("title"))+"%");
//        criteria.andEqualTo("isDeleteTime","0");
        SGoodsDto sGoodsDto = new SGoodsDto();
        sGoodsDto.setIsDeleteTime(0);
        sGoodsDto.setTitle(parameters.get("name"));
        sGoodsDto.setCategoryId(Integer.parseInt(parameters.get("categoryId")==null || parameters.get("categoryId").equals("") ? "" : parameters.get("categoryId")));
        List<SGoodsDto> userList = sGoodsMapper.getUserList(sGoodsDto);


//        List<SGoods> sGoods = sGoodsMapper.selectByExample(example);
        return new PageInfo<>(userList);
    }

    public void delete(Map<String, String> parameters) {
        CHECK_GOODS_DELETE(parameters);
        SGoods sGoods = new SGoods();
        sGoods.setId(Integer.parseInt(parameters.get("id")));
        sGoods.setIsDeleteTime(100000);
        sGoodsMapper.updateByPrimaryKeySelective(sGoods);
//        sGoodsMapper.deleteByPrimaryKey(parameters.get("id"));

//        Example example = new Example(SGoodsPhoto.class);
//        Example.Criteria criteria = example.createCriteria();
//        criteria.andEqualTo("goodsId", parameters.get("id"));
//        sGoodsPhotoMapper.deleteByExample(example);
//
//        Example exampleGoodsCategoryJoin = new Example(SGoodsPhoto.class);
//        Example.Criteria criteriaGoodsCategoryJoin = example.createCriteria();
//        criteriaGoodsCategoryJoin.andEqualTo("goodsId", parameters.get("id"));
//        sGoodsCategoryJoinMapper.deleteByExample(exampleGoodsCategoryJoin);



    }

    public void updateState(Map<String ,String> parameters) {
        CHECK_GOODS_UPDATE_STATE(parameters);
        SGoods sGoods = new SGoods();
        sGoods.setId(Integer.parseInt(parameters.get("id")));
        sGoods.setIsShelves(new Byte(parameters.get("isShelves")));
        sGoodsMapper.updateByPrimaryKeySelective(sGoods);
    }

    public void updateRecommend(Map<String, String> parameters) {
        CHECK_GOODS_UPDATE_RECOMMEND(parameters);
        SGoods sGoods = new SGoods();
        sGoods.setId(Integer.parseInt(parameters.get("id")));
        sGoods.setIsHomeRecommended(new Byte(parameters.get("isHomeRecommended")));
        sGoodsMapper.updateByPrimaryKeySelective(sGoods);

    }
}
